package com.briup.gsauestore.web.listener;

import com.briup.gsauestore.bean.Banner;
import com.briup.gsauestore.bean.Shop;
import com.briup.gsauestore.bean.vo.CategoryVO;
import com.briup.gsauestore.service.IBannerService;
import com.briup.gsauestore.service.ICategoryService;
import com.briup.gsauestore.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.List;

/**
 * @author adam
 * @date 2022/1/13
 */
@Component
public class ApplicationListener implements ServletContextListener {
    @Autowired
    private IShopService shopService;
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private IBannerService bannerService;

    @Value("${nginx.path}")
    private  String path;
    @Override
//应用创建的时候会执行的方法
//    商品分类查询   categories
//    特价商品  discountList
//    其他所有商品   shops
//    轮播图   banners
    public void contextInitialized(ServletContextEvent sce) {
//        获取应用对象
        ServletContext application = sce.getServletContext();
        List<Shop> shops = shopService.findAllShops();
        List<Shop> discountList = shopService.findDiscount();
        application.setAttribute("shops",shops);
        application.setAttribute("discountList",discountList);
        application.setAttribute("path",path);
        List<Banner> banners = bannerService.findActiveBanners();
        application.setAttribute("banners",banners);
        List<CategoryVO> list = categoryService.findAllCategory();
        application.setAttribute("categories",list);

    }
}
